/*
       Licensed to the Apache Software Foundation (ASF) under one
       or more contributor license agreements.  See the NOTICE file
       distributed with this work for additional information
       regarding copyright ownership.  The ASF licenses this file
       to you under the Apache License, Version 2.0 (the
       "License"); you may not use this file except in compliance
       with the License.  You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing,
       software distributed under the License is distributed on an
       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       KIND, either express or implied.  See the License for the
       specific language governing permissions and limitations
       under the License.
*/
package com.ciwong.epaper.modules.cordva;


import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.bookstore.ui.DuiBaActivity;
import com.ciwong.epaper.modules.cordva.bean.ErrorBook;
import com.ciwong.epaper.modules.cordva.dao.CordvaDao;
import com.ciwong.epaper.util.EConstants;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.epaper.util.LogOutUtil;
import com.ciwong.epaper.util.SerializableManager;
import com.ciwong.epaper.widget.NewLoginDialog;
import com.ciwong.eventbus.EventBus;
import com.ciwong.libs.utils.NetworkUtils;
import com.ciwong.libs.utils.volley.BaseRequest;
import com.ciwong.libs.utils.volley.HttpHelper;
import com.ciwong.libs.widget.CWToast;
import com.ciwong.mobilelib.bean.UserInfoBase;
import com.ciwong.mobilelib.i.BaseCallBack;
import com.ciwong.mobilelib.widget.CWDialog;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaArgs;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;


import static com.ciwong.mobilelib.utils.BaseJumpManager.GO_BACK_ID;
import static com.ciwong.mobilelib.utils.BaseJumpManager.INTENT_FLAG_SOURCE;


public class ActivityPlugin extends CordovaPlugin {

    //听说模考的类型来源
    public static final int SOURCE_TYPE_LSW = 3;
    private CallbackContext callbackContext = null;
    private static final String TAG = "ActivityPlugin";
    public boolean execute(String action, CordovaArgs args, final CallbackContext callbackContext) throws JSONException {
        this.callbackContext = callbackContext;
        String callbackid = callbackContext.getCallbackId();
        Log.e(TAG, "execute action: "+action );
        if (action.equals("AddErrorBook")) {
            String arg = args.getString(0);
            EventBus.getDefault().post(new SubmitEvent(arg, "AddErrorBook", null, null, callbackid));
            return true;

        } else if (action.equals("SelectErrorBook")) {
            JSONObject jsonObject = new JSONObject(args.getString(0));
            //String brandId=jsonObject.getString("");
            long pIndex;
            long psize;
            if (jsonObject.has("pageIndex") && jsonObject.has("pageSize")) {
                pIndex = jsonObject.getLong("pageIndex");
                psize = jsonObject.getLong("pageSize");
                //subjectId暂时传１　
                selectErrorBook(EApplication.BRAND_ID + "", pIndex + "", psize + "", "1", callbackContext);
            }
            return true;
        } else if (action.equals("DelectErrorBook")) {
            JSONObject jsonObject = new JSONObject(args.getString(0));
            String versionId = jsonObject.getString("versionId");
            if (NetworkUtils.isOnline()) {
                delectErrorBook(EApplication.BRAND_ID + "", versionId, callbackContext);

            } else {
                callbackContext.success(0);
                CWToast.makeText(this.cordova.getActivity(), R.string.pull_to_refresh_network_error, Toast.LENGTH_LONG, true)
                        .setToastType(CWToast.TOAST_TYPE_ALERT).show();
            }
            return true;

        } else if (action.equals("OpenVideo")) {
            openViedo(args, callbackContext);
            return true;
        } else if (action.equals("Submit")) {//提交作业
            String score;
            String remainNum;

            if (args.isNull(2)) {
                remainNum = args.getString(1);
                score = "-1";
            } else {
                score = args.getString(1);
                remainNum = args.getString(2);
            }
            String arg = args.getString(0);


            EventBus.getDefault().post(new SubmitEvent(arg, "Submit", remainNum, score));
            callbackContext.success(1);
            return true;

        } else if (action.equals("closeWin")) {//关闭H5页面

            return true;
        } else if (action.equals("SetTitle")) {
            String arg = args.getString(0);
            if (null != arg) {
                EventBus.getDefault().post(new SubmitEvent(arg, "SetTitle"));
            }
            return true;
        } else if (action.equals("CallToPay")) {
            //这次CallToPay为前端调用的方法名
            String arg = args.getString(0);
            if (null != arg) {
                EventBus.getDefault().post(new SubmitEvent(arg, "CallToPay"));//支付
            }
            return true;
        } else if (action.equals("GetUserInfo")) {//用户信息
            String arg = args.getString(0);
            JSONObject jsonObject = new JSONObject(arg);
            if (jsonObject.has("source")) {
                int s = jsonObject.getInt("source");
                if (s == 3) {
                    EventBus.getDefault().post(new SubmitEvent(arg, "GetUserInfo", callbackContext.getCallbackId()));//获取用户信息 包括作业ID
                } else {
                    BaseRequest.VerifyInfo verifyInfo = BaseRequest.getVerifyInfo();
                    JSONObject temp = new JSONObject();
                    temp.put("clientId", verifyInfo.getClientId());
                    temp.put("accessToken", verifyInfo.getAccessToken());
                    temp.put("openid", verifyInfo.getOpenId());
                    temp.put("oauth_version", verifyInfo.getOauthVersion());
                    temp.put("scope", verifyInfo.getScope());
                    temp.put("clientip", verifyInfo.getClientIp());
                    temp.put("termtype", verifyInfo.getTermType());
                    temp.put("brandId", EApplication.BRAND_ID);
                    UserInfoBase userInfo = EApplication.getInstance().getUserInfoBase();
                    if (null != userInfo)
                        temp.put("userId", userInfo.getUserId());
                    callbackContext.success(temp.toString());
                }
            }
            return true;
        } else if (action.equals("GetJsonData")) {//取本地资源
            String arg = args.getString(0);
            if (null != args) {
                EventBus.getDefault().post(new SubmitEvent(arg, "GetJsonData", callbackContext.getCallbackId()));
            }
            return true;
        } else if (action.equals("IsCanOpenVideo")) {//判断视频播放权限方法
            String arg = args.getString(0);
            if (null != args) {
                EventBus.getDefault().post(new SubmitEvent(arg, "IsCanOpenVideo", callbackContext.getCallbackId()));
            }

            return true;
        } else if (action.equals("GetVideoList")) {
            String arg = args.getString(0);
            if (null != args) {
                EventBus.getDefault().post(new SubmitEvent(arg, "GetVideoList", callbackContext.getCallbackId()));
            }
            return true;
        } else if (action.equals("OpenVideos")) {//打开多个视频
            String arg = args.getString(0);
            if (null != args) {
                EventBus.getDefault().post(new SubmitEvent(arg, "OpenVideos", callbackContext.getCallbackId()));
            }
            return true;
        } else if (action.equals("GetVideoRequestParams")) {//获取视频讲解请求会与不会状态参数
            String arg = args.getString(0);
            if (null != args) {
                EventBus.getDefault().post(new SubmitEvent(arg, "GetVideoRequestParams", callbackContext.getCallbackId()));
            }
            return true;
        } else if (action.equals("SendDelegateRequest")) {//H5代理请求服务器方法
            String arg = args.getString(0);
            if (null != arg) {
                //H5代理请求服务器方法
                HtmlRequest(arg, callbackContext);
            }
            return true;
        } else if (action.equals("GetNetState")) {
            //获取当前网络状态
            String arg = args.getString(0);
            getNetState(arg, callbackContext);

            return true;
        } else if (action.equals("GetWorkStatus")) {
            //获取作业批改状态的值
            String arg = args.getString(0);
            EventBus.getDefault().post(new SubmitEvent(arg, "GetWorkStatus", callbackContext.getCallbackId()));
            return true;
        } else if (action.equals("JumpToOldListen_Speak")) {
            //JumpToOldLinsten_Speak
            EventBus.getDefault().post(new SubmitEvent(args.getString(0), "JumpToOldListen_Speak", callbackContext.getCallbackId()));
        } else if (action.equals("JumpToOldRead")) {
            EventBus.getDefault().post(new SubmitEvent(args.getString(0), "JumpToOldRead", callbackContext.getCallbackId()));
            return true;
        }else if (action.equals("onRevised")) {
            EventBus.getDefault().post(new SubmitEvent(args.getString(0), "onRevised", callbackContext.getCallbackId()));
            return true;
        }  else if (action.equals("GetComments")) {
            EventBus.getDefault().post(new SubmitEvent(args.getString(0), "GetComments", callbackContext.getCallbackId()));
            return true;

        } else if (action.equals("JumpAgreementCancel")){
            EventBus.getDefault().post(new SubmitEvent(args.getString(0), "JumpAgreementCancel", callbackContext.getCallbackId()));
            return true;
        }else if (action.equals("reformQuestion")) {
            //听说模考重做题目

            String arg = args.getString(0);
            JSONObject jsonObject = new JSONObject(arg);
            if (jsonObject.has("source")) {
                int s = jsonObject.getInt("source");
                if (s == SOURCE_TYPE_LSW) {
                    EventBus.getDefault().post(new SubmitEvent(arg, "reformQuestion", callbackContext.getCallbackId()));//获取用户信息 包括作业ID

                }
            }
            return true;
        } else if (action.equals("JumpToAppRead")) {
            String arg = args.getString(0);
            EventBus.getDefault().post(new SubmitEvent(arg, "JumpToAppRead", null, null, callbackid));
            return true;
        } else if (action.equals("SelectPageWord")) {
            String arg = args.getString(0);
            EventBus.getDefault().post(new SubmitEvent(arg, "SelectPageWord", null, null, callbackid));
            return true;
        }else if (action.equals("TakePhotoForText")) {
            String arg = args.getString(0);
            EventBus.getDefault().post(new SubmitEvent(arg, "TakePhotoForText", null, null, callbackid));
            return true;

        }
//        else if(action.equals("SetTitle")){
//            EventBus.getDefault().post(new SubmitEvent(args.getString(0),"SetTitle",callbackContext.getCallbackId()));//设置标题
//            return true;
//        }
        else if (action.equals("getDoWorkInfo")) {
            //听说模考重做题目
            String arg = args.getString(0);
            JSONObject jsonObject = new JSONObject(arg);
            if (jsonObject.has("source")) {
                int s = jsonObject.getInt("source");
                if (s == SOURCE_TYPE_LSW) {
                    EventBus.getDefault().post(new SubmitEvent(arg, "getDoWorkInfo", callbackContext.getCallbackId()));//获取用户信息 包括作业ID
                }
            }
            return true;
        } else if (action.equals("GetShareInfo")) {
//            EventBus.getDefault().post(new SubmitEvent(args.getString(0), "GetShareInfo", callbackContext.getCallbackId()));
            return true;
        } else if (action.equals("GetOnlineExamination")) {//获取在线作答资源:GetOnlineExamination
//            SubmitEvent submitEvent = new SubmitEvent(args.getString(0), "GetOnlineExamination", callbackContext);
            SubmitEvent submitEvent = new SubmitEvent(args.getString(0), "GetOnlineExamination", callbackContext.getCallbackId());
            EventBus.getDefault().post(submitEvent);
            return true;
        } else if (action.equals("GetOnlineAnswer")) {
            SubmitEvent submitEvent = new SubmitEvent(args.getString(0), "GetOnlineAnswer", callbackContext.getCallbackId());
            EventBus.getDefault().post(submitEvent);
            return true;
        } else if (action.equals("SetOnlineAnswer")) {
            SubmitEvent submitEvent = new SubmitEvent(args.getString(0), "SetOnlineAnswer", callbackContext.getCallbackId());
            EventBus.getDefault().post(submitEvent);
            return true;
        } else if (action.equals("SubmitOnlineAnswer")) {
            SubmitEvent submitEvent = new SubmitEvent(args.getString(0), "SubmitOnlineAnswer", callbackContext.getCallbackId());
            EventBus.getDefault().post(submitEvent);
            return true;
        } else if (action.equals("ExitOnlineAnswer")) {
            SubmitEvent submitEvent = new SubmitEvent(args.getString(0), "ExitOnlineAnswer", callbackContext.getCallbackId());
            EventBus.getDefault().post(submitEvent);
            return true;
        } else if (action.equals("GetOnlineKey")) {
            SubmitEvent submitEvent = new SubmitEvent(args.getString(0), "GetOnlineKey", callbackContext.getCallbackId());
            EventBus.getDefault().post(submitEvent);
            return true;
        } else if (action.equals("TakePhoto")) {
            SubmitEvent submitEvent = new SubmitEvent(args.getString(0), "TakePhoto", callbackContext.getCallbackId());
            EventBus.getDefault().post(submitEvent);
            return true;
        }else if(action.equals("TakePhotoForPad")){
            SubmitEvent submitEvent = new SubmitEvent(args.getString(0), "TakePhotoForPad", callbackContext.getCallbackId());
            EventBus.getDefault().post(submitEvent);
            return true;
        }else if(action.equals("IFinishedReading")){
            SubmitEvent submitEvent = new SubmitEvent(args.getString(0), "IFinishedReading", callbackContext.getCallbackId());
            EventBus.getDefault().post(submitEvent);
            return true;
        }else if(action.equals("ReminderLogin")){
            if(EConstants.IS_YOUKE){
                NewLoginDialog newLoginDialog=new NewLoginDialog(this.cordova.getActivity());
                newLoginDialog.showLoginDialog();
            }
        }else if(action.equals("JumpServiceDetails")){
            SubmitEvent submitEvent = new SubmitEvent(args.getString(0), "JumpServiceDetails", callbackContext.getCallbackId());
            EventBus.getDefault().post(submitEvent);
            return true;
        }else if(action.equals("DisplayShare")){
           /* String arg="";
            if (!args.isNull(0))
                arg=args.getString(0);
            SubmitEvent submitEvent = new SubmitEvent(arg, "DisplayShare", callbackContext.getCallbackId());
            EventBus.getDefault().post(submitEvent);*/
            callbackContext.success(1);
            return true;
        } else if(action.equals("GetDubbingInfo")){//获取微信分享内容
            String arg = args.getString(0);
            if (null != arg) {
                EventBus.getDefault().post(new SubmitEvent(arg, "GetDubbingInfo"));
            }
            return true;
        }else if(action.equals("IsFirstPK")){
            String arg="";
            if (!args.isNull(0))
                arg=args.getString(0);
            SubmitEvent submitEvent = new SubmitEvent(arg, "IsFirstPK", callbackContext.getCallbackId());
            EventBus.getDefault().post(submitEvent);
        }else if(action.equals("JumpToNewWeb")){
            try {
                String arg = args.getString(0);
                JSONObject jsonObject = new JSONObject(arg);
                if (jsonObject.has("url")) {
                    String url=jsonObject.getString("url");
                    Intent intent = new Intent(cordova.getActivity(), DuiBaActivity.class);
                    intent.putExtra(INTENT_FLAG_SOURCE, true);
                    intent.putExtra(GO_BACK_ID, R.string.go_back);
                    intent.putExtra(IntentFlag.INTENT_FLAG_URL,url);
                    cordova.getActivity().startActivity(intent);
                }
            }catch (Exception e){
                e.getStackTrace();
            }

        }
        return false;
    }


    /**
     * 获取当前网络状态
     */
    private void getNetState(String arg, final CallbackContext callbackContext) {
        try {
            if (NetworkUtils.isOnline()) {
                callbackContext.success(1);
            } else {
                callbackContext.success(0);
            }
        } catch (Exception e) {
            e.getStackTrace();
        }
    }


    /**
     * H5代理请求服务器方法
     */
    private void HtmlRequest(String arg, final CallbackContext callbackContext) {
        try {
            JSONObject jsonObject = new JSONObject(arg);
            AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
            //
            asyncHttpClient.setTimeout(30 * 1000);
            RequestParams params = new RequestParams();
            String url = null;
            if (jsonObject.has("url")) {
                url = jsonObject.getString("url");
            }
            if (url == null) {
                return;
            }
            if (jsonObject.has("auth")) {
                //
                int auth = jsonObject.getInt("auth");
                if (auth == 1) {//需要认证
                    url = buildUrl(true, url);
                } else if (auth == 0) {
                    url = buildUrl(false, url);
                }
            }
            if (jsonObject.has("param")) {
                JSONObject jsonObject1 = jsonObject.getJSONObject("param");
                Iterator<String> keys = jsonObject1.keys();
                while (keys.hasNext()) {
                    String key = (String) keys.next();
                    String value = (String) jsonObject1.getString(key);
                    params.put(key, value);
                }
            }
            if (jsonObject.has("type")) {
                String method = jsonObject.getString("type");
                if ("GET".equals(method)) {
                    asyncHttpClient.get(url, params, new JsonHttpResponseHandler() {
                        @Override
                        public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {


                            //做非空判断
                            if (null != errorResponse) {
                                if (errorResponse.has("errcode")) {
                                    try {
                                        int errcode = errorResponse.getInt("errcode");
                                        if (errcode == 17 || errcode == 27) {
                                            forceBackToAlert(errcode);
                                        } else {
                                            callbackContext.success(errorResponse);
                                        }
                                    } catch (Exception e) {
                                        e.getStackTrace();
                                    }

                                } else {
                                    callbackContext.success(errorResponse);
                                }

                            } else {
                                callbackContext.success("");
                            }

                        }

                        @Override
                        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                            //做非空判断
                            if (null != response) {
                                if (response.has("errcode")) {
                                    try {
                                        int errcode = response.getInt("errcode");
                                        if (errcode == 17 || errcode == 27) {
                                            forceBackToAlert(errcode);
                                        } else {
                                            callbackContext.success(response);
                                        }
                                    } catch (Exception e) {
                                        e.getStackTrace();
                                    }

                                } else {
                                    callbackContext.success(response);
                                }
                            } else {
                                callbackContext.success("");
                            }
                        }
                    });


                } else if ("POST".equals(method)) {
                    asyncHttpClient.post(url, params, new JsonHttpResponseHandler() {
                                @Override
                                public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                                    //做非空判断
                                    if (null != response) {
                                        if (response.has("errcode")) {
                                            try {
                                                int errcode = response.getInt("errcode");
                                                if (errcode == 17 || errcode == 27) {
                                                    forceBackToAlert(errcode);
                                                } else {
                                                    callbackContext.success(response);
                                                }
                                            } catch (Exception e) {
                                                e.getStackTrace();
                                            }

                                        } else {
                                            callbackContext.success(response);
                                        }
                                    } else {
                                        callbackContext.success("");
                                    }
                                }

                                @Override
                                public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                                    //做非空判断
                                    if (null != errorResponse) {
                                        if (errorResponse.has("errcode")) {
                                            try {
                                                int errcode = errorResponse.getInt("errcode");
                                                if (errcode == 17 || errcode == 27) {
                                                    forceBackToAlert(errcode);
                                                } else {
                                                    callbackContext.success(errorResponse);
                                                }
                                            } catch (Exception e) {
                                                e.getStackTrace();
                                            }

                                        } else {
                                            callbackContext.success(errorResponse);
                                        }
                                    } else {
                                        callbackContext.success("");
                                    }
                                }
                            }
                    );
                }

            }
        } catch (Exception e) {
            e.getStackTrace();
        }
    }


    /**
     * 打开视频
     *
     * @param args
     * @param callbackContext
     */
    private void openViedo(CordovaArgs args, final CallbackContext callbackContext) throws JSONException {
        String ar = null;

        if (null != args) {
            ar = args.getString(0);
            EventBus.getDefault().post(new SubmitEvent(ar, "OpenVideo", null, null, callbackContext.getCallbackId()));
        }
       /* if(!id.isEmpty()){
        // ViedoJumpManager.jumpToViedoPlayerActivity(11,callbackid,R.string.go_back,this.cordova.getActivity(),"namnm8tlle","e187b81fb6",true);
            ViedoJumpManager.jumpToViedoPlayerActivity(11,callbackid,R.string.go_back,this.cordova.getActivity(),"namnm8tlle",id,true);
            }*/
    }


    /**
     * 查询错题本
     *
     * @param brandId
     * @param pIndex
     * @param psize
     */
    private void selectErrorBook(String brandId, String pIndex, String psize, String subjectId, final CallbackContext callbackContext) {
        CordvaDao.getInstance().selectErrorBook(brandId, pIndex, psize, subjectId, new BaseCallBack() {
            @Override
            public void failed(int errorCode, Object data) {

            }

            @Override
            public void failed(Object data) {

            }


            @Override
            public void success(Object data) {
                ErrorBook errorBook = (ErrorBook) data;
                String htmlJson = new Gson().toJson(errorBook);

                if (errorBook != null && htmlJson != null) {
                    callbackContext.success(htmlJson);
                }
            }
        });
    }


    /**
     * 删除错题本
     *
     * @param brandId
     * @param versionId
     */

    private void delectErrorBook(String brandId, String versionId, final CallbackContext callbackContext) {
        CordvaDao.getInstance().delectErrorBook(brandId, versionId, new BaseCallBack() {
            @Override
            public void failed(int errorCode, Object data) {
                callbackContext.success(2);
            }

            @Override
            public void failed(Object data) {
                callbackContext.success(2);
            }

            @Override
            public void success(Object data) {
                callbackContext.success(1);
            }

        });
    }


    public void callbackContext(String arg) {
        callbackContext.success(arg);
    }


    /**
     * 请求地址初始化
     *
     * @param isVerifyAction 是否添加Verify动作
     * @return 返回请求地址
     */
    public String buildUrl(boolean isVerifyAction, String url) {
        if (url != null) {
            if (isVerifyAction) {
                if (BaseRequest.getVerifyInfo() == null) {
                    if (!url.endsWith("?") && !url.contains("?")) {
                        //url += "?";
                    }
                } else {
                    url += HttpHelper.getVerifyStr(BaseRequest
                            .getVerifyInfo());
                }
            } else {
                if (!url.endsWith("?") && !url.contains("?")) {
                    //url += "?";
                }
            }
        }
        return url;
    }

    //H5 代理请求如果TOken失效 退出
    public void forceBackToAlert(int errorCode) {

        if (this.cordova.getActivity() != null && this.cordova.getActivity() instanceof Activity) {
            final CWDialog dialogEx = new CWDialog(((Activity) this.cordova.getActivity()));

            //别人登录了强制退出了
            final Activity activity = (Activity) this.cordova.getActivity();
            if (activity.isFinishing()) {
                return;
            }
            String userId = SerializableManager.getInstance().getUserId(true);
            if (null != userId && userId.contains(",")) {
                //去除对于得,
                userId = userId.replace(",", "");
            }
            if (errorCode == 17) {
                try {
                    SimpleDateFormat sd = new SimpleDateFormat("yyyy年MM月dd日");
                    dialogEx.setMessage("你的账号(" + userId + ")于" + sd.format(new Date()) + "在另外一台设备登录。如非本人操作，则密码可能已经泄露，建议及时修改密码。");
                    dialogEx.setPositiveButton("重新登录", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            LogOutUtil.logOut(activity);
                            dialogEx.dismiss();
                        }
                    });
                    dialogEx.show();
                    return;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (errorCode == 27) {
                try {
                    dialogEx.setMessage("用户信息已过期，请重新登录");
                    dialogEx.setPositiveButton("重新登录", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            LogOutUtil.logOut(activity);
                            dialogEx.dismiss();
                        }
                    });
                    dialogEx.show();
                    return;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
